package org.rcloud.io.controller;

import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.ArticleCategoryWithBLOBs;
import org.rcloud.io.service.ArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RequestMapping(value = "/category")
@RestController
public class ArticleCategoryController {

    @Autowired
    private ArticleCategoryService acs;

    @RequestMapping(value = "/findByTitle")
    @ResponseBody
    @CrossOrigin
    public PageVo<ArticleCategoryWithBLOBs> findByTitle(String title) {
        return acs.findByTitle(title);
    }

    @RequestMapping(value = "/addThemeGame")
    @ResponseBody
    @CrossOrigin
    public ResultVo addThemeGame(String title, String content) {
        ArticleCategoryWithBLOBs category = new ArticleCategoryWithBLOBs();
        Date date = new Date();
        category.setPid(102);
        category.setSlug(title);
        category.setTitle(title);
        category.setType("game");
        category.setFlag(title);
        category.setContent(content);
        category.setMetaKeywords(title);
        category.setMetaDescription(title);
        category.setCreated(date);
        category.setModified(date);


        return acs.addGame(category);
    }

    @RequestMapping(value = "/updateGame")
    @ResponseBody
    @CrossOrigin
    public ResultVo updateThemeGame(String title, Integer id, String content) {
        ArticleCategoryWithBLOBs category = new ArticleCategoryWithBLOBs();
        category.setTitle(title);
        category.setId(id);
        category.setContent(content);


        return acs.update(category);
    }

    @RequestMapping(value = "/findById")
    @ResponseBody
    @CrossOrigin
    public ResultVo findById(Integer id) {

        return acs.findById(id);
    }

    @RequestMapping(value = "/findByPid")
    @ResponseBody
    @CrossOrigin
    public PageVo<ArticleCategoryWithBLOBs> findByPid(Integer pid) {

        return acs.selectByPid(pid);
    }

    @RequestMapping(value = "/findGame")
    @ResponseBody
    @CrossOrigin
    public PageVo<ArticleCategoryWithBLOBs> findGame() {

        List<ArticleCategoryWithBLOBs> list = acs.selectByPid(101).getInfo();
        List<ArticleCategoryWithBLOBs> info = acs.selectByPid(102).getInfo();
        try {
            for (ArticleCategoryWithBLOBs a: info) {
                list.add(a);
            }
        } finally {
            return PageVo.setOk(list);
        }

    }
}
